/**
 * Copyright (c) 2011 SameGoal LLC.
 * All Rights Reserved.
 * Author: Andy Hochhaus <ahochhaus@samegoal.com>
 */

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview RTE field implementation.
 * @suppress {accessControls|visibility}
 */

goog.provide('sg.net.http.server.comet.demo');
goog.provide('sg.net.http.server.comet.demo.ChannelHandler');

goog.require('goog.Timer');
goog.require('goog.Uri');
goog.require('goog.dom');
goog.require('goog.dom.ViewportSizeMonitor');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.json');
goog.require('goog.net.BrowserChannel');
goog.require('goog.net.BrowserChannel.Handler');
goog.require('goog.net.ChannelRequest');
goog.require('goog.net.tmpnetwork');
goog.require('goog.string.StringBuffer');
goog.require('goog.structs.Map');
goog.require('goog.ui.Button');
goog.require('goog.ui.Component');
goog.require('goog.ui.Component.EventType');
goog.require('goog.ui.Container');
goog.require('goog.ui.Control');
goog.require('goog.ui.FlatButtonRenderer');
goog.require('goog.ui.Prompt');
goog.require('goog.ui.SplitPane');
goog.require('goog.ui.Textarea');


/*
sg.net.http.server.comet.demo.debugWindow =
   new goog.debug.FancyWindow('debugWindow');
sg.net.http.server.comet.demo.debugWindow.setEnabled(true);
sg.net.http.server.comet.demo.debugWindow.init();
*/


/** @type {goog.ui.Container} */
sg.net.http.server.comet.demo.user_list = new goog.ui.Container();


/** @type {goog.ui.Component} */
sg.net.http.server.comet.demo.chat_ui = new goog.ui.Component();


/** @type {goog.ui.Textarea} */
sg.net.http.server.comet.demo.text_area = new goog.ui.Textarea('');
sg.net.http.server.comet.demo.text_area.setMinHeight(50);
sg.net.http.server.comet.demo.text_area.setMaxHeight(100);
sg.net.http.server.comet.demo.chat_ui.addChild(
    sg.net.http.server.comet.demo.text_area, true);


/** @type {goog.ui.Button} */
sg.net.http.server.comet.demo.send_button = new goog.ui.Button(
    'Send', goog.ui.FlatButtonRenderer.getInstance());
sg.net.http.server.comet.demo.chat_ui.addChild(
    sg.net.http.server.comet.demo.send_button, true);


/** @type {goog.ui.Component} */
sg.net.http.server.comet.demo.chat_console = new goog.ui.Component();
sg.net.http.server.comet.demo.chat_ui.addChild(
    sg.net.http.server.comet.demo.chat_console, true);


/** @type {goog.ui.SplitPane} */
sg.net.http.server.comet.demo.splitpane = new goog.ui.SplitPane(
    sg.net.http.server.comet.demo.user_list,
    sg.net.http.server.comet.demo.chat_ui,
    goog.ui.SplitPane.Orientation.HORIZONTAL);
sg.net.http.server.comet.demo.splitpane.setInitialSize(175);
sg.net.http.server.comet.demo.splitpane.render();


sg.net.http.server.comet.demo.user_list.getElement().innerHTML +=
    '<a href="#" onclick="disconnect(); return false;">disconnect</a>';
sg.net.http.server.comet.demo.chat_console.getElement().innerHTML +=
    '<div id="chat_pane" width="100%"></div>';
goog.events.listen(sg.net.http.server.comet.demo.send_button,
                   goog.ui.Component.EventType.ACTION,
                   sg.net.http.server.comet.demo.send);


/** @type {goog.dom.ViewportSizeMonitor} */
sg.net.http.server.comet.demo.viewport_size_monitor =
    new goog.dom.ViewportSizeMonitor();


/**
 * Callback to resize split pane when viewport is resized by the user.
 * @param {goog.dom.ViewportSizeMonitor=} opt_event Event.
 */
sg.net.http.server.comet.demo.resizeCallback = function(opt_event) {
  var size = sg.net.http.server.comet.demo.viewport_size_monitor.getSize();
  sg.net.http.server.comet.demo.splitpane.setSize(size);
};


goog.events.listen(sg.net.http.server.comet.demo.viewport_size_monitor,
                   goog.events.EventType.RESIZE,
                   sg.net.http.server.comet.demo.resizeCallback);
sg.net.http.server.comet.demo.resizeCallback();


/**
 * Log message to the console.
 * @param {string} message String to log.
 */
sg.net.http.server.comet.demo.msg = function(message) {
  var msg_div = goog.dom.createDom('DIV', undefined,
      goog.dom.htmlToDocumentFragment(message));
  var el = /** @type {!Element} */ (goog.dom.getElement('chat_pane'));
  goog.dom.append(el, msg_div);
  el.scrollTop = el.scrollHeight;
};



/**
 * Browser channel handler.
 * @constructor
 * @extends {goog.net.BrowserChannel.Handler}
 */
sg.net.http.server.comet.demo.ChannelHandler = function() {};
goog.inherits(sg.net.http.server.comet.demo.ChannelHandler,
              goog.net.BrowserChannel.Handler);


/** @override */
sg.net.http.server.comet.demo.ChannelHandler.prototype.channelOpened =
    function(browserChannel) {
  sg.net.http.server.comet.demo.msg('**channelOpened');
};


/** @override */
sg.net.http.server.comet.demo.ChannelHandler.prototype.channelError = function(
    browserChannel, error) {
  sg.net.http.server.comet.demo.msg('**channelError');
};


/** @override */
sg.net.http.server.comet.demo.ChannelHandler.prototype.channelClosed =
    function(browserChannel, opt_pendingMaps, opt_undeliveredMaps) {
  sg.net.http.server.comet.demo.msg('**channelClosed');
};


/** @override */
sg.net.http.server.comet.demo.ChannelHandler.prototype.channelHandleArray =
    function(browserChannel, array) {
  if (array[0] == 'new') {
    var cont = new goog.ui.Control(array[1]);
    cont.setId(array[2]);
    sg.net.http.server.comet.demo.user_list.addChild(cont, true);
  } else if (array[0] == 'message') {
    sg.net.http.server.comet.demo.msg('<b>' + array[1] + '</b>: ' + array[2]);
  } else if (array[0] == 'delete') {
    sg.net.http.server.comet.demo.user_list.removeChild(array[1], true);
  }
};


/** @override */
sg.net.http.server.comet.demo.ChannelHandler.prototype.getNetworkTestImageUri =
    function(browserChannel) {
  sg.net.http.server.comet.demo.msg('**getNetworkTestImageUri');
  var base = '/third_party/closure-library/closure/goog/';
  return new goog.Uri(base + 'images/tree/cleardot.gif');
};

var handler = new sg.net.http.server.comet.demo.ChannelHandler();
//var channelDebug = new goog.net.ChannelDebug();
var channel = new goog.net.BrowserChannel('8');
channel.setHandler(handler);
//channel.setChannelDebug(channelDebug);
channel.connect('channel/test', 'channel/bind', {});


/**
 * Send a chat message.
 */
sg.net.http.server.comet.demo.send = function() {
  var text = goog.string.htmlEscape(
      sg.net.http.server.comet.demo.text_area.getValue());
  sg.net.http.server.comet.demo.text_area.setValue('');
  channel.sendMap({'message': text});
};


/**
 * Set username.
 * @param {string} response User prompt response.
 */
sg.net.http.server.comet.demo.send_username = function(response) {
  channel.sendMap({'user': response ? response : 'Anonmous'});
};

var p = new goog.ui.Prompt('Username Required',
                           'What is your name?',
                           sg.net.http.server.comet.demo.send_username);
p.setVisible(true);


/**
 * Disconnect browserchannel.
 */
sg.net.http.server.comet.demo.disconnect = function() {
  sg.net.http.server.comet.demo.user_list.dispose();
  channel.disconnect();
};


goog.exportSymbol('disconnect', sg.net.http.server.comet.demo.disconnect);
